"""
插入大量数据
"""
import random

import MySQLdb

from utils import random_string

conn = MySQLdb.connect(host='47.104.31.138', port=3306,
                       user='root', password='Luohao.618',
                       database='hrs', charset='utf8')
try:
    with conn.cursor() as cursor:
        for i in range(10):
            params = []
            for j in range(1, 101):
                username = f'user{100 * i + j}'
                password = random_string(random.randint(8, 20))
                params.append((username, password))
            cursor.executemany(
                'insert into tb_user (username, password) values (%s, %s)',
                params
            )
    # 4. 提交事务
    conn.commit()
except MySQLdb.MySQLError as err:
    print(err)
    # 4. 回滚事务
    conn.rollback()
finally:
    # 5. 关闭数据库连接（释放资源）
    conn.close()
